package zhong.mp3player;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import zhong.download.HttpDownloader;
import zhong.model.Mp3Info;
import zhong.parsexml.Mp3ListContentHandler;
import zhong.utils.FileUtils;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Mp3ListActivity extends ListActivity {

	private static final int UPDATE = 1;
	private static final int ABOUT = 2;
	private List<Mp3Info> mp3Infos;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.updateListView(AppConstant.Url.xmlUrl);
		setContentView(R.layout.remote_mp3_list);
		
		
		
		
	}

	public boolean onCreateOptionsMenu(Menu menu) {

		// TODO Auto-generated method stub
		menu.add(0, UPDATE, 1, R.string.update);
		menu.add(0, ABOUT, 2, R.string.about);
		
		return super.onCreateOptionsMenu(menu);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		if (item.getItemId() == UPDATE) {
			Mp3ListActivity.this.updateListView(AppConstant.Url.xmlUrl);

		}

		else if (item.getItemId() == ABOUT) {

			System.out.println(item.getItemId());

		}
		return super.onOptionsItemSelected(item);
	}



	
	private List<Mp3Info> parse(String xmlStr) {
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		List<Mp3Info> infos = new ArrayList<Mp3Info>();
		try {
			
			XMLReader xmlReader = saxParserFactory.newSAXParser()
					.getXMLReader();
			
			Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(
					infos);
			xmlReader.setContentHandler(mp3ListContentHandler);
			
			
			xmlReader.parse(new InputSource(new StringReader(xmlStr)));

			 for (Iterator iterator = infos.iterator(); iterator.hasNext();) {
			 Mp3Info mp3Info = (Mp3Info) iterator.next();
			 System.out.println(mp3Info.toString());
			 }

		} catch (Exception e) {
			e.printStackTrace();
		}

		return infos;
	}
	
	
	
	private void updateListView(String urlStr) {
//		downXMLHandler=new DownXMLHandler();
//		HttpDownloader httpDownloader = new HttpDownloader();
//		String listxml = httpDownloader.download(urlStr);
		DownXML downXML=new DownXML();
		downXML.start();
	}
	
	
	Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			String xml=(String)msg.obj;
			System.out.println("xml----->"+xml);
			 
			mp3Infos = parse(xml);
			FileUtils fileUtils=new FileUtils();
			SimpleAdapter listAdapter=fileUtils.buildSimpleAdapter(mp3Infos,Mp3ListActivity.this);
			
	        setListAdapter(listAdapter);
			
		}
		
	};
	
	class DownXML extends Thread{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			HttpDownloader httpDownloader = new HttpDownloader();
			String listxml=httpDownloader.download(AppConstant.Url.xmlUrl);
			
			Message msg=handler.obtainMessage();
			msg.obj=listxml;
			handler.sendMessage(msg);
//			msg.obj=httpDownloader.download(urlStr);
//			downXMLHandler.sendMessage(msg);
		}
		
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Mp3Info mp3Info=mp3Infos.get(position);
		
//		System.out.println("mp3info--->"+mp3Info);
//		Intent intent=new Intent();
//		intent.putExtra("mp3Info", mp3Info);
//		intent.setClass(this, DownloadService.class);
//		startService(intent);
		Intent intent=new Intent();
		intent.putExtra("mp3Info", mp3Info);
		intent.setClass(this, DownloadActivity.class);
		this.startActivity(intent);
		
	}
	
	
}
